
function LookupCurrentGridItem()
{
var oGridItem = crmGrid.InnerGrid.SelectedRecords[0];
if (IsNull(oGridItem))
{
return null;
}
else
{
return crmGrid.InnerGrid.rows[oGridItem[2]];
}
}


function ViewArticle(hideEmailButton)
{
var oGridItem = crmGrid.InnerGrid.SelectedRecords[0];
if (!IsNull(oGridItem) && !IsNull(oGridItem[0]))
{
var url = "?id=" + CrmEncodeDecode.CrmUrlEncode(oGridItem[0]);
if (hideEmailButton)
{
url += "&HideEmailButton=true";
}
openFrmObj(url, buildWinName(oGridItem[0]), KbArticle);
}
}



function CreateLookupItem(tr)
{
if (!IsNull(tr.oid))
{
var lookupItems = new LookupItems();
var li = new LookupItem();
var td = tr.cells[1];

li.id = tr.oid;
li.name = td.innerText;
li.html = tr.cells[0].innerHTML + td.innerHTML;
li.type = KbArticle;
li.values = new Array();
li.keyValues = new Array();




var columns = tr.parentElement.parentElement.getElementsByTagName("col");
var len = columns.length;
if (len > 1)
{
for (var i = 1; i < len; ++i)
{
li.keyValues[new String(columns[i].name)] = new LookupItemData(columns[i].name, tr.cells[i].innerText)
li.values.push(new LookupItemData(columns[i].name, tr.cells[i].innerText));
}
}
lookupItems.items.push(li);
return lookupItems;
}
return null;
}

function KbSearchJsWindowOnLoad()
{

frmSearchInt.SearchSubject.value = "";
crmGrid.OnBeforeFormLoad = applychanges;
crmGrid.SetParameter("disableDblClick", "0");
crmGrid.OnSelectionChange = OnGridSelectionChange;
OnGridSelectionChange();
}

window.attachEvent('onload', KbSearchJsWindowOnLoad);

function OnGridSelectionChange()
{
butBegin.disabled = btnViewArticle.disabled = IsNull(crmGrid.InnerGrid.SelectedRecords[0]);
}


function applychanges()
{
var oGridItem = LookupCurrentGridItem();
if (!IsNull(oGridItem))
{
window.returnValue = CreateLookupItem(oGridItem);
}
window.event.returnValue = false;
window.close();
}


function removeValue()
{
window.returnValue = new LookupItems();
window.event.returnValue = false;
window.close();
}


function cancel()
{
window.close();
}
